<html>
<head><meta charset="utf-8"><title>Should NonZero* implement AsRef/Deref to the underlying type · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Should.20NonZero*.20implement.20AsRef.2FDeref.20to.20the.20underlying.20type.html">Should NonZero* implement AsRef/Deref to the underlying type</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="243650061"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Should%20NonZero%2A%20implement%20AsRef/Deref%20to%20the%20underlying%20type/near/243650061" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Should.20NonZero*.20implement.20AsRef.2FDeref.20to.20the.20underlying.20type.html#243650061">(Jun 23 2021 at 13:20)</a>:</h4>
<p>I'm using NonZero* types a lot (Mostly NonZeroUsize), and a lot of times I need to pass it to functions that accept the underlying type (say usize), and I find myself witing <code>&amp;n.get()</code> a lot, and I wondered maybe they should implement AsRef/Deref so they'll coerce into that type when needed</p>
<p>(I also need <code>&amp;[NonZeroUsize]</code> -&gt; <code>&amp;[usize]</code> but that will have to wait for the SafeTransmute project)</p>



<a name="243665254"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Should%20NonZero%2A%20implement%20AsRef/Deref%20to%20the%20underlying%20type/near/243665254" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Should.20NonZero*.20implement.20AsRef.2FDeref.20to.20the.20underlying.20type.html#243665254">(Jun 23 2021 at 15:04)</a>:</h4>
<p>If you don't mind my asking, why &amp;usize and not just usize?</p>



<a name="243670815"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Should%20NonZero%2A%20implement%20AsRef/Deref%20to%20the%20underlying%20type/near/243670815" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Should.20NonZero*.20implement.20AsRef.2FDeref.20to.20the.20underlying.20type.html#243670815">(Jun 23 2021 at 15:41)</a>:</h4>
<p><span class="user-mention silent" data-user-id="224471">Lokathor</span> <a href="#narrow/stream/219381-t-libs/topic/Should.20NonZero*.20implement.20AsRef.2FDeref.20to.20the.20underlying.20type/near/243665254">said</a>:</p>
<blockquote>
<p>If you don't mind my asking, why &amp;usize and not just usize?</p>
</blockquote>
<p>because I operate a lot on libstd containers that contains usize (slices, maps, vecs, more). and they accept &amp;T</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>